import com.arcadedb.console.Console;
import indi.hjhk.arcadedb.datatrans.*;
import indi.hjhk.global.GlobalVariables;
import indi.hjhk.mysql.driver.mysqlHandler;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseTrans {

//    private static void transData(Console console, ArcadedbSchemaVertex vertex, mysqlHandler mysql){
//        String name=vertex.getName();
//        try{
//            console.parse(String.format("TRUNCATE TYPE %s", name), false);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        long offset=0, size=1000, totalElapsed=0;
//        while (true) {
//            long subElapsed=0;
//            String sql=String.format("SELECT * FROM %s LIMIT %d, %d", name, offset, size);
//            ResultSet rs = mysql.query(sql);
//            if (rs == null) break;
//            int count=0;
//            try{
//                while (rs.next()){
//                    console.parse(vertex.resolveFromRS(rs).getSQLCreate(), false);
//                    count++;
//                    Long elapsed= GlobalVariables.getLong("elapsed");
//                    if (elapsed!=null) subElapsed+=elapsed;
//                }
//            } catch (SQLException | IOException e) {
//                System.err.printf("Problem occurred when transferring '%s' from MySQL to ArcadeDB at offset %d (+%d)\n", name, offset, count);
//                throw new RuntimeException(e);
//            }
//            if (count==0) break;
//            System.out.println(String.format("Transferring '%s' from MySQL to ArcadeDB, offset: %d~%d (+%d), timeSpent: %dms", name, offset, offset+size-1, count, subElapsed));
//            offset+=size;
//            totalElapsed+=subElapsed;
//        }
//        System.out.println(String.format("Transferred '%s' from MySQL to ArcadeDB using %dms", name, totalElapsed));
//    }
//
//    public static void main(String[] args) {
//        try {
//            Console console = new Console(false);
//            mysqlHandler mysql=new mysqlHandler().setIpAddr("127.0.0.1").setdbName("test");
//
//            console.parse("drop database ./databases/test", false);
//            console.parse("create database test", false);
//            //console.parse("connect test", false);
//
//            ArcadedbSchemaVertex snapShot = new ArcadedbSchemaVertex("SnapShot");
//            snapShot.addProperty("id", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("carId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
//            snapShot.addProperty("predId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
//            snapShot.addProperty("succId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
//            snapShot.addProperty("averageSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("currentSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("lastPeriodTime", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("lastPeriodMinMile", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("TimeExpired", PropertyType.MYSQL_BIT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("timestamp", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("expired", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
//            snapShot.addProperty("passid", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
//            snapShot.addIndex(IndexType.MYSQL_INDEX, "passid");
//            console.parse(snapShot.getSQLCreateSchema(), false);
//            transData(console, snapShot, mysql);
//
////            ArcadedbSchemaVertex snapShotPure = new ArcadedbSchemaVertex("SnapShotPure");
////            snapShotPure.addProperty("id", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("carId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotPure.addProperty("predId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotPure.addProperty("succId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotPure.addProperty("averageSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("currentSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("lastPeriodTime", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("lastPeriodMinMile", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("TimeExpired", PropertyType.MYSQL_BIT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("timestamp", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("expired", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotPure.addProperty("passid", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            console.parse(snapShotPure.getSQLCreateSchema());
////            transData(console, snapShotPure, mysql);
////
////            ArcadedbSchemaVertex snapShotPureOrder = new ArcadedbSchemaVertex("SnapShotPureOrder");
////            snapShotPureOrder.addProperty("id", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            snapShotPureOrder.addProperty("predId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotPureOrder.addProperty("succId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotPureOrder.addProperty("timestamp", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotPureOrder.addProperty("expired", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotPureOrder.addProperty("passid", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            console.parse(snapShotPureOrder.getSQLCreateSchema());
////            transData(console, snapShotPureOrder, mysql);
////
////            ArcadedbSchemaVertex snapShotTest = new ArcadedbSchemaVertex("SnapShotTest");
////            snapShotTest.addProperty("id", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("carId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotTest.addProperty("predId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotTest.addProperty("succId", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_DEFAULT);
////            snapShotTest.addProperty("averageSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("currentSpeed", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("lastPeriodTime", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("lastPeriodMinMile", PropertyType.MYSQL_INT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("TimeExpired", PropertyType.MYSQL_BIT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("timestamp", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("expired", PropertyType.MYSQL_BIGINT, Constraint.MYSQL_NOTNULL);
////            snapShotTest.addProperty("passid", PropertyType.MYSQL_TINYTEXT, Constraint.MYSQL_NOTNULL);
////            console.parse(snapShotTest.getSQLCreateSchema());
////            transData(console, snapShotTest, mysql);
//
//            console.close();
//            mysql.disConnect();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
